package io.renren.sdapi.service.impl;

import cn.hutool.core.util.StrUtil;
import io.renren.membermanage.entity.ImageGenerateParam;
import io.renren.membermanage.service.ImageGenerateParamService;
import io.renren.sdapi.dto.httprequestparam.image2image.encapsulation.AlwaysonScripts;
import io.renren.sdapi.dto.httprequestparam.image2image.encapsulation.Args;
import io.renren.sdapi.dto.httprequestparam.image2image.encapsulation.ControlNet;
import io.renren.sdapi.service.HandleParamService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class HandleParamServiceImpl implements HandleParamService {
    
    @Resource
    private ImageGenerateParamService imageGenerateParamService;

    /**
     * 线稿
     * @return
     */
    public AlwaysonScripts getAlwaysonScriptsWithLine(String type) {
        AlwaysonScripts alwaysonScripts = new AlwaysonScripts();
        ControlNet controlNet = new ControlNet();
        List<Args> args = new ArrayList<>();
        List<ImageGenerateParam> imageGenerateParam = imageGenerateParamService.getImageGenerateParam(type);
        Args arg = new Args();
        Map<String, String> collect = imageGenerateParam.stream().collect(Collectors.toMap(ImageGenerateParam::getParamName, ImageGenerateParam::getParamValue));
        arg.setEnabled(collect.get("enabled"));
        arg.setPixel_perfect(collect.get("pixel_perfect"));
        arg.setModule(collect.get("module"));
        arg.setModel(collect.get("model"));
        arg.setWeight(Double.parseDouble(collect.get("weight")));
        arg.setControl_mode(collect.get("control_mode"));
        arg.setGuidance_end(Double.parseDouble(collect.get("guidance_end")));
        args.add(arg);
        controlNet.setArgs(args);
        alwaysonScripts.setControlNet(controlNet);
        return alwaysonScripts;
    }

    /**
     * 白膜
     */
    public AlwaysonScripts getAlwaysonScriptsWithAlbuginea(String type, String albugineaType) {
        List<ImageGenerateParam> imageGenerateParam = imageGenerateParamService.getImageGenerateParam(type);
        List<ImageGenerateParam> filers = imageGenerateParam.stream().filter(c -> c.getParamFlag().equals(albugineaType)).toList();
        AlwaysonScripts alwaysonScripts = new AlwaysonScripts();
        ControlNet controlNet = new ControlNet();
        List<Args> args = new ArrayList<>();
        Map<String, String> collect = filers.stream().collect(Collectors.toMap(ImageGenerateParam::getParamName, ImageGenerateParam::getParamValue));
        Args arg = new Args();
        arg.setEnabled(collect.get("enabled"));
        arg.setPixel_perfect(collect.get("pixel_perfect"));
        arg.setModule(collect.get("module"));
        arg.setModel(collect.get("model"));
        arg.setWeight(Double.parseDouble(collect.get("weight")));
        arg.setControl_mode(collect.get("control_mode"));
        arg.setGuidance_end(Double.parseDouble(collect.get("guidance_end")));
        args.add(arg);
        controlNet.setArgs(args);
        alwaysonScripts.setControlNet(controlNet);
        return alwaysonScripts;
    }

    /**
     * 细化
     */
    public AlwaysonScripts getAlwaysonScriptsWithDetailing(String type) {
        AlwaysonScripts alwaysonScripts = new AlwaysonScripts();
        ControlNet controlNet = new ControlNet();
        List<Args> args = new ArrayList<>();
        List<ImageGenerateParam> imageGenerateParam = imageGenerateParamService.getImageGenerateParam(type);
        Args arg = new Args();
        Map<String, String> collect = imageGenerateParam.stream().collect(Collectors.toMap(ImageGenerateParam::getParamName, ImageGenerateParam::getParamValue));
        arg.setEnabled(collect.get("enabled"));
        arg.setPixel_perfect(collect.get("pixel_perfect"));
        arg.setModule(collect.get("module"));
        arg.setModel(collect.get("model"));
        arg.setWeight(Double.parseDouble(collect.get("weight")));
        arg.setControl_mode(collect.get("control_mode"));
        arg.setGuidance_end(Double.parseDouble(collect.get("guidance_end")));
        args.add(arg);
        controlNet.setArgs(args);
        alwaysonScripts.setControlNet(controlNet);
        return alwaysonScripts;
    }

    /**
     * 灵感融合
     */
    public AlwaysonScripts getAlwaysonScriptsWithInspiration(String type, double weight1, double weight3, String image, String paramImage) {
        AlwaysonScripts alwaysonScripts = new AlwaysonScripts();
        ControlNet controlNet = new ControlNet();
        List<Args> args = new ArrayList<>();
        List<ImageGenerateParam> imageGenerateParam = imageGenerateParamService.getImageGenerateParam(type);
        Map<Integer, List<ImageGenerateParam>> map = imageGenerateParam.stream().collect(Collectors.groupingBy(ImageGenerateParam::getParamGroup));
        map.forEach((k, v) -> {
            Args arg = new Args();
            Map<String, String> collect = v.stream().collect(Collectors.toMap(ImageGenerateParam::getParamName, ImageGenerateParam::getParamValue));
            arg.setEnabled(collect.get("enabled"));
            arg.setPixel_perfect(collect.get("pixel_perfect"));
            arg.setModule(collect.get("module"));
            arg.setModel(collect.get("model"));
            arg.setGuidance_end(1);
            if (k == 1){
                arg.setWeight(weight1);
                arg.setImage(paramImage);
                args.add(arg);
            }else if (k == 3){
                arg.setWeight(weight3);
                arg.setImage(image);
                args.add(arg);
            }else {
                arg.setWeight(Double.parseDouble(collect.get("weight")));
                arg.setImage(paramImage);
                args.add(arg);
            }
        });
        controlNet.setArgs(args);
        alwaysonScripts.setControlNet(controlNet);
        return alwaysonScripts;
    }

    /**
     * 重绘
     */
    public AlwaysonScripts getAlwaysonScriptsWithRedrawing(String type, String redrawingType){
        AlwaysonScripts alwaysonScripts = new AlwaysonScripts();
        ControlNet controlNet = new ControlNet();
        List<Args> args = new ArrayList<>();
        List<ImageGenerateParam> imageGenerateParam = imageGenerateParamService.getImageGenerateParam(type);
        Map<String, List<ImageGenerateParam>> map = imageGenerateParam.stream().collect(Collectors.groupingBy(ImageGenerateParam::getParamFlag));
        String[] split = redrawingType.split(",");
        for (int i = 0; i < split.length; i++) {
            String str = StrUtil.subBefore(split[i], "-", false);
            if (map.containsKey(str) && null != map.get(str)){
                List<ImageGenerateParam> list = map.get(str);
                Map<String, String> collect = list.stream().collect(Collectors.toMap(ImageGenerateParam::getParamName, ImageGenerateParam::getParamValue));
                Args arg = new Args();
                arg.setEnabled(collect.get("enabled"));
                arg.setPixel_perfect(collect.get("pixel_perfect"));
                arg.setModule(collect.get("module"));
                arg.setControl_mode(collect.get("control_mode"));
                arg.setModel(collect.get("model"));
                if ("wireframe".equals(str)){
                    arg.setWeight(Double.parseDouble(StrUtil.subAfter(split[i], "-", false)));
                    args.add(arg);
                }
                if ("profundity".equals(str)){
                    arg.setWeight(Double.parseDouble(StrUtil.subAfter(split[i], "-", false)));
                    args.add(arg);
                }
            }
        }
        controlNet.setArgs(args);
        alwaysonScripts.setControlNet(controlNet);
        return alwaysonScripts;
    }
}
